package com.nari.ysphd.base;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jeremyfeinstein.gson.Meeting;
import com.jeremyfeinstein.gson.OnResponse;
import com.jeremyfeinstein.okhttputil.callback.StringCallback;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.Comm;
import com.jeremyfeinstein.utils.ToastUtil;
import com.jeremyfeinstein.utils.YCHShttpAPI;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.nari.ysphd.ui.IPConfigActivity;
import com.nari.ysphd.ui.TimeOutActivity;
import com.nari.ysphd.util.ActivityManager;

import java.util.ArrayList;
import java.util.List;

import nari.mip.core.Platform;
import okhttp3.Call;

/**
 * Create by sunxunchao on 2019/3/29
 */
public abstract class BaseMeetingFragment extends BaseFragment {

    private static final String TAG = BaseMeetingFragment.class.getName();
//    protected View rootView;

    protected ListView mListView;
    protected AbsAdapter adapter;
    protected SwipeToLoadLayout mSwipeToLoadLayout;

    private int page = 1;

//    private LinearLayout empty;
//    private TextView empty_content;


    protected int getLayoutId() {
        return R.layout.fragment_meetings;
    }

    protected void initViews(View rootView) {
        mListView = (ListView) rootView.findViewById(R.id.swipe_target);
        mSwipeToLoadLayout = (SwipeToLoadLayout) rootView.findViewById(R.id.swipeToLoadLayout);
//        empty = (LinearLayout) rootView.findViewById(R.id.empty);
//        empty_content = (TextView) rootView.findViewById(R.id.empty_content);

        adapter = getAdapter();
        mListView.setAdapter(adapter);

        mSwipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                initData(getMeetingOfWords());
            }
        });

        mSwipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                initData(getMeetingOfWords());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
        //创建新的会议之后，刷新三个列表，获取最新数据
        mSwipeToLoadLayout.setRefreshing(true);
    }

    protected abstract AbsAdapter getAdapter();

    protected void initData(String meetingName) {
        if (Comm.getSpecialcharacter(meetingName)) {
            ToastUtil.showToast("输入了非法字符！");
            mSwipeToLoadLayout.setRefreshing(false);
            return;
        }
        YCHShttpAPI.GetMeetingList(getActivity(), getMeetingListType(), String.valueOf(page), meetingName, new StringCallback() {
            @Override
            public void onResponse(String response) {
                OnResponse onResponse = YCHShttpAPI.getOnResponse(response);
                Log.d(TAG, "getMeetingList type ====>>>" + getMeetingListType());
                if ("0".equals(onResponse.getResultCode())) {
                    List<Meeting> meetingList = onResponse.getMeetingList();
                    if (meetingList == null || meetingList.size() == 0) {
                        page--;
                        ToastUtil.showLong(MyApp.getApp(), onResponse.getErrorMessage());
                    }
                    if (mSwipeToLoadLayout.isRefreshing()) {
                        adapter.setItem(meetingList);
                        mSwipeToLoadLayout.setRefreshing(false);
                        if (!mSwipeToLoadLayout.isLoadMoreEnabled()) {
                            mSwipeToLoadLayout.setLoadMoreEnabled(true);
                        }
                    } else if (mSwipeToLoadLayout.isLoadingMore()) {
                        int count = adapter.getCount();
                        adapter.addItems(meetingList);
                        mListView.setSelection(count);
                        mSwipeToLoadLayout.setLoadingMore(false);
                    }
                } else if ("1".equals(onResponse.getResultCode())) {
                    if (mSwipeToLoadLayout.isRefreshing()) {
                        List<Meeting> meetings = new ArrayList<>();
                        adapter.setItem(meetings);
                        mSwipeToLoadLayout.setRefreshing(false);
                        if (!mSwipeToLoadLayout.isLoadMoreEnabled()) {
                            mSwipeToLoadLayout.setLoadMoreEnabled(true);
                        }
                    } else if (mSwipeToLoadLayout.isLoadingMore()) {
                        mSwipeToLoadLayout.setLoadingMore(false);
                        if (mSwipeToLoadLayout.isLoadMoreEnabled()) {
                            mSwipeToLoadLayout.setLoadMoreEnabled(false);
                        }
                    }
                } else if ("-2".equals(onResponse.getResultCode())){
                    Intent intent = new Intent(getActivity(), TimeOutActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } else{
//                    ToastUtil.showToast(onResponse.getErrorMessage());
//                    empty_content.setText(String.valueOf(onResponse.getErrorMessage()));
                    if (mSwipeToLoadLayout.isRefreshing()) {
                        mSwipeToLoadLayout.setRefreshing(false);
                    }
                    if (mSwipeToLoadLayout.isLoadingMore()) {
                        mSwipeToLoadLayout.setLoadingMore(false);
                    }
                    if (mSwipeToLoadLayout.isLoadMoreEnabled()) {
                        mSwipeToLoadLayout.setLoadMoreEnabled(false);
                    }
//                    if (AppConstants.TAG_LOGINOUT.equals(onResponse.getErrorMessage())){
//                        ActivityManager.getInstance().finishActivityExcept(IPConfigActivity.class);
//                        Platform.getCurrent().stop(MyApp.getApp());
//                        ToastUtil.showLong(MyApp.getApp(),onResponse.getErrorMessage());
//                    }
                }

            }

            @Override
            public void onError(Call call, int statusCode, Exception e) {
                page--;
                if (mSwipeToLoadLayout.isRefreshing()) {
                    mSwipeToLoadLayout.setRefreshing(false);
                } else if (mSwipeToLoadLayout.isLoadingMore()) {
                    mSwipeToLoadLayout.setLoadingMore(false);
                    mSwipeToLoadLayout.setLoadMoreEnabled(false);
                }
//                ToastUtil.showToast("请求出错了");
                Log.e(TAG, "onError: ", e);
            }
        });
    }

    protected abstract String getMeetingListType();

    /**
     * 互动名称关键字搜索
     * 空字符串时表示搜索全部互动
     *
     * @return
     */
    protected abstract String getMeetingOfWords();
}
